<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>matcher.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_pattern_matcher.js.html">matcher.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview DOM pattern matcher.  Allows for simple searching of DOM
<a name="line17"></a> * using patterns descended from {@link goog.dom.pattern.AbstractPattern}.
<a name="line18"></a> *
<a name="line19"></a> * @author robbyw@google.com (Robby Walker)
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.dom.pattern.Matcher&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.dom.TagIterator&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom.pattern.MatchType&#39;);
<a name="line26"></a>goog.require(&#39;goog.iter&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>// TODO(robbyw): Allow for backtracks of size &gt; 1.
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * Given a set of patterns and a root node, this class tests the patterns in
<a name="line35"></a> * parallel.
<a name="line36"></a> *
<a name="line37"></a> * It is not (yet) a smart matcher - it doesn&#39;t do any advanced backtracking.
<a name="line38"></a> * Given the pattern &lt;code&gt;DIV, SPAN&lt;/code&gt; the matcher will not match
<a name="line39"></a> * &lt;code&gt;DIV, DIV, SPAN&lt;/code&gt; because it starts matching at the first
<a name="line40"></a> * &lt;code&gt;DIV&lt;/code&gt;, fails to match &lt;code&gt;SPAN&lt;/code&gt; at the second, and never
<a name="line41"></a> * backtracks to try again.
<a name="line42"></a> *
<a name="line43"></a> * It is also possible to have a set of complex patterns that when matched in
<a name="line44"></a> * parallel will miss some possible matches.  Running multiple times will catch
<a name="line45"></a> * all matches eventually.
<a name="line46"></a> *
<a name="line47"></a> * @constructor
<a name="line48"></a> */
<a name="line49"></a>goog.dom.pattern.Matcher = function() {
<a name="line50"></a>  this.patterns_ = [];
<a name="line51"></a>  this.callbacks_ = [];
<a name="line52"></a>};
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Array of patterns to attempt to match in parallel.
<a name="line57"></a> *
<a name="line58"></a> * @type {Array.&lt;goog.dom.pattern.AbstractPattern&gt;}
<a name="line59"></a> * @private
<a name="line60"></a> */
<a name="line61"></a>goog.dom.pattern.Matcher.prototype.patterns_;
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * Array of callbacks to call when a pattern is matched.  The indexing is the
<a name="line66"></a> * same as the {@link #patterns_} array.
<a name="line67"></a> *
<a name="line68"></a> * @type {Array.&lt;Function&gt;}
<a name="line69"></a> * @private
<a name="line70"></a> */
<a name="line71"></a>goog.dom.pattern.Matcher.prototype.callbacks_;
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * Adds a pattern to be matched.  The callback can return an object whose keys
<a name="line76"></a> * are processing instructions.
<a name="line77"></a> *
<a name="line78"></a> * @param {goog.dom.pattern.AbstractPattern} pattern The pattern to add.
<a name="line79"></a> * @param {Function} callback Function to call when a match is found.  Uses
<a name="line80"></a> *     the above semantics.
<a name="line81"></a> */
<a name="line82"></a>goog.dom.pattern.Matcher.prototype.addPattern = function(pattern, callback) {
<a name="line83"></a>  this.patterns_.push(pattern);
<a name="line84"></a>  this.callbacks_.push(callback);
<a name="line85"></a>};
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>/**
<a name="line89"></a> * Resets all the patterns.
<a name="line90"></a> *
<a name="line91"></a> * @private
<a name="line92"></a> */
<a name="line93"></a>goog.dom.pattern.Matcher.prototype.reset_ = function() {
<a name="line94"></a>  for (var i = 0, len = this.patterns_.length; i &lt; len; i++) {
<a name="line95"></a>    this.patterns_[i].reset();
<a name="line96"></a>  }
<a name="line97"></a>};
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>/**
<a name="line101"></a> * Test the given node against all patterns.
<a name="line102"></a> *
<a name="line103"></a> * @param {goog.dom.TagIterator} position A position in a node walk that is
<a name="line104"></a> *     located at the token to process.
<a name="line105"></a> * @return {boolean} Whether a pattern modified the position or tree
<a name="line106"></a> *     and its callback resulted in DOM structure or position modification.
<a name="line107"></a> * @private
<a name="line108"></a> */
<a name="line109"></a>goog.dom.pattern.Matcher.prototype.matchToken_ = function(position) {
<a name="line110"></a>  for (var i = 0, len = this.patterns_.length; i &lt; len; i++) {
<a name="line111"></a>    var pattern = this.patterns_[i];
<a name="line112"></a>    switch (pattern.matchToken(position.node, position.tagType)) {
<a name="line113"></a>      case goog.dom.pattern.MatchType.MATCH:
<a name="line114"></a>      case goog.dom.pattern.MatchType.BACKTRACK_MATCH:
<a name="line115"></a>        var callback = this.callbacks_[i];
<a name="line116"></a>
<a name="line117"></a>        // Callbacks are allowed to modify the current position, but must
<a name="line118"></a>        // return true if the do.
<a name="line119"></a>        if (callback(pattern.matchedNode, position, pattern)) {
<a name="line120"></a>          return true;
<a name="line121"></a>        }
<a name="line122"></a>
<a name="line123"></a>      default:
<a name="line124"></a>        // Do nothing.
<a name="line125"></a>        break;
<a name="line126"></a>    }
<a name="line127"></a>  }
<a name="line128"></a>
<a name="line129"></a>  return false;
<a name="line130"></a>};
<a name="line131"></a>
<a name="line132"></a>
<a name="line133"></a>/**
<a name="line134"></a> * Match the set of patterns against a match tree.
<a name="line135"></a> *
<a name="line136"></a> * @param {Node} node The root node of the tree to match.
<a name="line137"></a> */
<a name="line138"></a>goog.dom.pattern.Matcher.prototype.match = function(node) {
<a name="line139"></a>  var position = new goog.dom.TagIterator(node);
<a name="line140"></a>
<a name="line141"></a>  this.reset_();
<a name="line142"></a>
<a name="line143"></a>  goog.iter.forEach(position, function() {
<a name="line144"></a>    while (this.matchToken_(position)) {
<a name="line145"></a>      // Since we&#39;ve moved, our old pattern statuses don&#39;t make sense any more.
<a name="line146"></a>      // Reset them.
<a name="line147"></a>      this.reset_();
<a name="line148"></a>    }
<a name="line149"></a>  }, this);
<a name="line150"></a>};
</pre>


</body>
</html>
